<template>
  <div>
    <div
      style="border: 2px solid skyblue;margin: 20px auto;width:20%;text-align:center;font-size:40px;background:skyblue"
    >个人中心</div>
    <div
      style="border: 3px solid black;width:60%;height:500px;margin: 150px auto;"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    >
      <div style="margin:120px 190px">
        <span style="font-size:20px;">
          新密码:
          <el-input placeholder="请输入密码" v-model="password" show-password style="width:80%"></el-input>
        </span>
        <br />
        <br />
        <br />
        <span style="font-size:20px;">
          确认密码:
          <el-input placeholder="请再次输入密码" v-model="isPassword" show-password style="width:80%"></el-input>
        </span>
        <br />
        <br />
        <div style="margin:40px 200px">
          <el-button type="primary" round @click="updateMima" style="width:100px">确认修改</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "changePassword",
  data() {
    return {
      password: "",
      isPassword: "",
      loading: false
    };
  },
  methods: {
    async updateMima() {
      this.loading = true;
      if (this.password != this.isPassword) {
        alert("两次密码不一样，请重新输入！!!");
        this.loading = false;
        this.password = "";
        this.isPassword = "";
      } else if (this.password == "" || this.isPassword == "") {
        alert("密码不能为空");
        this.loading = false;
      } else {
        await this.$API
          .call("/changePassword", "post", "", { password: this.password })
          .then(res => {
            if (res.data != "修改成功") {
              alert("修改失败");
              this.password = "";
              this.isPassword = "";
            } else {
              alert("修改成功,请重新登录");
              sessionStorage.removeItem('token');
                sessionStorage.removeItem('code');
                sessionStorage.removeItem('identity');
                sessionStorage.removeItem('grade');
              this.$router.push({
                    path:'/loginPage'
                })
            }
            this.loading = false;
          })
          .catch(() => {
            alert("修改错误，请联系管理员");
          });
      }
    }
  }
};
</script>>
  
<style lang='less' scoped>

</style>